home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / copy.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  4KB  |  202 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include "gltk.h"
  33.  
  34. GLenum doubleBuffer, directRender;
  35. GLint windW, windH;
  36.  
  37. char *fileName = 0;
  38. TK_RGBImageRec *image;
  39. float point[3];
  40. float zoom;
  41. GLint x, y;
  42.  
  43. static void Init(void)
  44. {
  45.  
  46.   glClearColor(0.0, 0.0, 0.0, 0.0);
  47.  
  48.   x = 0;
  49.   y = windH;
  50.   zoom = 1.8;
  51. }
  52.  
  53. static void Reshape(int width, int height)
  54. {
  55.  
  56.   windW = (GLint) width;
  57.   windH = (GLint) height;
  58.  
  59.   glViewport(0, 0, windW, windH);
  60.  
  61.   glMatrixMode(GL_PROJECTION);
  62.   glLoadIdentity();
  63.   gluOrtho2D(0, windW, 0, windH);
  64.   glMatrixMode(GL_MODELVIEW);
  65. }
  66.  
  67. static GLenum Key(int key, GLenum mask)
  68. {
  69.  
  70.   switch (key) {
  71.     case TK_ESCAPE:
  72.       tkQuit();
  73.     case TK_Z:
  74.       zoom += 0.2;
  75.       break;
  76.     case TK_z:
  77.       zoom -= 0.2;
  78.       if (zoom < 0.2) {
  79.     zoom = 0.2;
  80.       }
  81.       break;
  82.     default:
  83.       return GL_FALSE;
  84.   }
  85.   return GL_TRUE;
  86. }
  87.  
  88. static GLenum Mouse(int mouseX, int mouseY, GLenum button)
  89. {
  90.  
  91.   x = (GLint) mouseX;
  92.   y = (GLint) mouseY;
  93.   return GL_TRUE;
  94. }
  95.  
  96. static void Draw(void)
  97. {
  98.  
  99.   glClear(GL_COLOR_BUFFER_BIT);
  100.  
  101.   point[0] = (windW / 2) - (image->sizeX / 2);
  102.   point[1] = (windH / 2) - (image->sizeY / 2);
  103.   point[2] = 0;
  104.   glRasterPos3fv(point);
  105.  
  106.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  107.   glPixelZoom(1.0, 1.0);
  108.   glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  109.            image->data);
  110.  
  111.   point[0] = (float)x;
  112.   point[1] = windH - (float)y;
  113.   point[2] = 0.0;
  114.   glRasterPos3fv(point);
  115.  
  116.   glPixelZoom(zoom, zoom);
  117.   glCopyPixels((windW / 2) - (image->sizeX / 2),
  118.            (windH / 2) - (image->sizeY / 2),
  119.            image->sizeX, image->sizeY, GL_COLOR);
  120.  
  121.   glFlush();
  122.  
  123.   if (doubleBuffer) {
  124.     tkSwapBuffers();
  125.   }
  126. }
  127.  
  128. static GLenum Args(int argc, char **argv)
  129. {
  130.   GLint i;
  131.  
  132.   doubleBuffer = GL_FALSE;
  133.   directRender = GL_TRUE;
  134.  
  135.   for (i = 1; i < argc; i++) {
  136.     if (strcmp(argv[i], "-sb") == 0) {
  137.       doubleBuffer = GL_FALSE;
  138.     }
  139.     else if (strcmp(argv[i], "-db") == 0) {
  140.       doubleBuffer = GL_TRUE;
  141.     }
  142.     else if (strcmp(argv[i], "-dr") == 0) {
  143.       directRender = GL_TRUE;
  144.     }
  145.     else if (strcmp(argv[i], "-ir") == 0) {
  146.       directRender = GL_FALSE;
  147.     }
  148.     else if (strcmp(argv[i], "-f") == 0) {
  149.       if (i + 1 >= argc || argv[i + 1][0] == '-') {
  150.     printf("-f (No file name).\n");
  151.     return GL_FALSE;
  152.       }
  153.       else {
  154.     fileName = argv[++i];
  155.       }
  156.     }
  157.     else {
  158.       printf("%s (Bad option).\n", argv[i]);
  159.       return GL_FALSE;
  160.     }
  161.   }
  162.   return GL_TRUE;
  163. }
  164.  
  165. void main(int argc, char **argv)
  166. {
  167.   GLenum type;
  168.  
  169.   if (Args(argc, argv) == GL_FALSE) {
  170.     tkQuit();
  171.   }
  172.  
  173.   if (fileName == 0) {
  174.     printf("No image file.\n");
  175.     tkQuit();
  176.   }
  177.  
  178.   image = tkRGBImageLoad(fileName);
  179.  
  180.   windW = 300;
  181.   windH = 300;
  182.   tkInitPosition(0, 0, windW, windH);
  183.  
  184.   type = TK_RGB;
  185.   type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  186.   type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  187.   tkInitDisplayMode(type);
  188.  
  189.   if (tkInitWindow("Copy Test") == GL_FALSE) {
  190.     tkQuit();
  191.   }
  192.  
  193.   Init();
  194.  
  195.   tkExposeFunc(Reshape);
  196.   tkReshapeFunc(Reshape);
  197.   tkKeyDownFunc(Key);
  198.   tkMouseDownFunc(Mouse);
  199.   tkDisplayFunc(Draw);
  200.   tkExec();
  201. }
  202.